<?php

namespace app\common\model;

use think\Cache;
use think\Model;

/**
 * 系统设置模型
 * Class Setting
 * @package app\common\model
 */
class _TreeBase extends Model
{

    //分类格式化数据
    private $tree_data = array();
    //目录树 数据
    private $format_tree = array();
    //子类id集合
    private $sub_ids = array();

    private $parents = array();
    protected $dispname,$pk,$pid;



    //获取所有父类
    public function getParents($parentid,$field='name',$pk='id',$pid = 'parent_id'){
        $this->pk = $pk;
        $this->pid = $pid;
        $this->_get_parent($parentid,$field);
        return $this->parents;
    }

    private function _get_parent($parentid,$field){
        $parent = $this->field(array($this->pk,$this->pid,$field))->where(array($this->pk=>$parentid))->find();
        if($parent) {
            $this->_get_parent($parent[$this->pid],$field);
            $this->parents[] = $parent;
        }
    }


    /**
     * 获取所有子类id
     * @access public
     * @param int $id
     * @param string $dispname 名称字段 默认是 'name'
     * @param string $pk 主键字段
     * @param string $pid 父类ID字段
     * @return array 子类ID集合
     */
    public function getSubIds($id,$pk='id',$pid = 'parent_id'){
        $this->pk=$pk;
        $this->pid = $pid;
        $this->_get_sub_id($id);
        return $this->sub_ids;
    }

    private function _get_sub_id($id){
        $ids = $this->field($this->pk)->where(array($this->pid=>$id))->select();
        if($ids){
            foreach ($ids as $value) {
                $this->sub_ids[] = $value[$this->pk];
                $this->_get_sub_id($value[$this->pk]);
            }
        }
    }

    /**
     * 把分类列表格式化为目录树
     * @access public
     * @param array $list 分类列表
     * @param int $root_id 根目录id
     * @param string $dispname 名称字段 默认是 'name'
     * @param string $pk 主键字段
     * @param string $pid 父类ID字段
     * @return array 目录树
     */
    function toFormatTree($list,$root_id=0,$break=-1,$dispname='name',$pk='id',$pid = 'parent_id')
    {
        if(is_array($list)) {
            foreach ($list as $value) {
                $this->tree_data[$value[$pk]] = $value;
            }
            $this->dispname = $dispname;
            $this->pk=$pk;
            $this->pid = $pid;
//            if(isset($list[0][$sort])) $this->sort = $sort;
//            $this->hide_id = $hide_id;
        }
        $this->_get_tree($root_id,0,$break);
        return $this->format_tree;
    }


    function toFormatArr($list,$root_id=0,$break=-1,$dispname='name',$pk='id',$pid = 'parent_id')
    {
        if(is_array($list)) {
            foreach ($list as $value) {
                $this->tree_data[$value[$pk]] = $value;
            }
            $this->dispname = $dispname;
            $this->pk=$pk;
            $this->pid = $pid;
//            if(isset($list[0][$sort])) $this->sort = $sort;
//            $this->hide_id = $hide_id;
        }
        $this->_get_arr($root_id,0,$break);
        return $this->format_tree;
    }

    //根目录id，本层数0开始，跳出层数：-1为递归所有层级
    private function _get_arr($root_id, $level = 0,$break=-1)
    {
        $child = $this->_get_child($root_id);
        if ($level > 0)
        {
            $item = $this->tree_data[$root_id];
            $ret = $break>0 && $level+1>$break;
            if ($level > 1)
            {
                $name = $item[$this->dispname];
                $item[$this->dispname] = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$level-1).'┝ '.$name;
            }
            $item['_child_count'] = $child && !$ret?count($child):0;
            array_push($this->format_tree,$item);
            if($ret) return;
        }

        if ($child)
        {
            ++$level;
            foreach ($child as $key=>$value)
                $this->_get_arr($key, $level,$break);
        }
    }



    //根目录id，本层数0开始，跳出层数：-1为递归所有层级
    private function _get_tree($root_id, $level = 0,$break=-1)
    {
        $child = $this->_get_child($root_id);
        if ($level > 0)
        {
            $item = $this->tree_data[$root_id];
            $ret = $break>0 && $level+1>$break;
            if ($level > 1)
            {
                $name = $item[$this->dispname];
                $item[$this->dispname] = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$level-1).'┝ '.$name;
            }
            $item['_child_count'] = $child && !$ret?count($child):0;
            array_push($this->format_tree,$item);
            if($ret) return;
        }

        if ($child)
        {
            ++$level;
            foreach ($child as $key=>$value)
                $this->_get_tree($key, $level,$break);
        }
    }



    private function _get_child($myid) {
        $newarr = array();
        if (is_array($this->tree_data)) {
            foreach ($this->tree_data as $k => $v) {
                if ($v[$this->pid] == $myid) {
                    $newarr[$v[$this->pk]] = $v;
                }
            }
        }
        return $newarr ? $newarr : false;
    }

}